package com.sinotn.ks.node.camera.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mysql.jdbc.StringUtils;
import com.sinotn.Page;
import com.sinotn.SinotnLogger;
import com.sinotn.gson.GsonFacade;
import com.sinotn.ks.node.camera.bean.CameraPhotoBean;
import com.sinotn.ks.node.camera.dao.cond.CameraPhotoCond;
import com.sinotn.ks.node.camera.service.CameraService;
import com.sinotn.ks.node.meta.service.RoomService;
import com.sinotn.ks.node.meta.service.ServerAuthService;
import com.sinotn.ks.node.meta.vo.ExamRoomVO;
import com.sinotn.web.RequestUtil;
import com.sinotn.web.ResponseUtil;

/**
 * 摄像监考Web接口
 *
 * @Title CameraAction.java
 * @Package com.sinotn.ks.node.camera.action
 * @Description Copyright: Copyright (c) 2015 Company:北京信诺软通
 *
 * @author <a href="mailto:xieqj@sinotn.com">谢启进</a>
 * @date 2017年6月17日 下午3:07:31
 * @version V1.0
 */
@Controller
@RequestMapping(value = "/plugin/camera")
public class CameraAction {
	@Autowired
	private CameraService cameraService;
	@Autowired
	private ServerAuthService serverAuthService;
	@Autowired
	private RoomService roomService;

	@RequestMapping(value = "/list.jspx")
	public void list(HttpServletRequest request, HttpServletResponse response) {
		try {
			int pageIdx = RequestUtil.getPageIdx(request);
			int pageSize = RequestUtil.getPageSize(request);
			CameraPhotoCond cond = new CameraPhotoCond();
			RequestUtil.populate(cond, request);
			StringBuilder sb = new StringBuilder();
			sb.append("{\"state\":0");
			String roomId = request.getParameter("roomId");
			if (roomId == null || roomId.length() == 0) {
				String id = request.getParameter("id");// 管理机ID
				if (id != null) {
					List<String> roomIds = this.serverAuthService.findRefRoomId(id);
					List<ExamRoomVO> rooms = this.roomService.findExamRoomByIds(roomIds);
					if (rooms != null && rooms.size() > 0) {
						sb.append(",\"rooms\":");
						GsonFacade.toJson(rooms, sb);
						int size = rooms.size();
						for (int i = 0; i < size; i++) {
							cond.addRoomId(rooms.get(i).getRoomId());
						}
					}
				}
			} else {
				String[] roomIds = roomId.split(";");
				for (int i = 0; i < roomIds.length; i++) {
					cond.addRoomId(roomIds[i]);
				}
			}

			Page page = this.cameraService.findCameraPhotoByPage(pageIdx, pageSize, cond);

			sb.append(",\"totalNum\":").append(page.getTotalRowNum());
			sb.append(",\"rows\":");
			GsonFacade.toJson(page.getRows(), sb);
			sb.append("}");
			ResponseUtil.renderJson(response, sb.toString());
			SinotnLogger.DEBUG.info(sb.toString());
		} catch (Throwable e) {
			SinotnLogger.DEBUG.error("分页查询考生监考照片数据发生系统错误", e);
			ResponseUtil.renderJson(response, "{\"state\":1}");
		}
	}

	@RequestMapping(value = "/getPhotoInfo.jspx")
	public void getPhotoInfo(HttpServletRequest request, HttpServletResponse response) {
		try {
			String id = request.getParameter("id");
			CameraPhotoBean bean = cameraService.findCameraPhotoInfo(id);
			StringBuilder sb = new StringBuilder();
			sb.append("{\"state\":0");
			sb.append(",\"data\":").append(GsonFacade.toJson(bean));
			sb.append("}");
			ResponseUtil.renderJson(response, sb.toString());
		} catch (Throwable e) {
			SinotnLogger.DEBUG.error("加载考生照片数据发生系统错误", e);
			ResponseUtil.renderJson(response, "{\"status\":1,\"errorMsg\":\"加载考生照片数据发生系统错误\"}");
		}
	}

	@RequestMapping(value = "/photoFixed.jspx")
	public void photoFixed(HttpServletRequest request, HttpServletResponse response) {
		try {
			String id = request.getParameter("id");
			String invigilatorId = request.getParameter("invigilatorId");
			String invigilatorName = request.getParameter("invigilatorName");
			String invigilatorPhone = request.getParameter("invigilatorPhone");
			String remark = request.getParameter("remark");
			String isCheat = request.getParameter("isCheat");
			byte fixed;
			if (isCheat != null && "true".equals(isCheat)) {
				fixed = (byte) 2;
			} else {
				fixed = (byte) 1;
			}
			if (StringUtils.isNullOrEmpty(id) || StringUtils.isNullOrEmpty(invigilatorName) || StringUtils.isNullOrEmpty(invigilatorPhone)) {
				ResponseUtil.renderJson(response, "{\"status\":1,\"errorMsg\":\"参数ID、监考员姓名、监考员名称不能为空\"}");
				return;
			}
			cameraService.fixCameraPhoto(id,invigilatorId, invigilatorName, invigilatorPhone, remark, fixed);
			ResponseUtil.renderJson(response, "{\"state\":0}");
		} catch (Throwable e) {
			SinotnLogger.DEBUG.error("处理监考照片时发生系统错误", e);
			ResponseUtil.renderJson(response, "{\"status\":1,\"errorMsg\":\"处理监考照片时发生系统错误\"}");
		}
	}

	@RequestMapping(value = "/getNormalPhotos.jspx")
	public void getNormalPhotos(HttpServletRequest request, HttpServletResponse response) {
		try {
			String examineeId = request.getParameter("examineeId");
			String roomId = request.getParameter("roomId");
			if (StringUtils.isNullOrEmpty(examineeId) || StringUtils.isNullOrEmpty(roomId)) {
				ResponseUtil.renderJson(response, "{\"status\":1, \"errorMsg\":\"参数错误，请联系系统管理员！\"}");
			}
			StringBuilder sb = new StringBuilder();
			sb.append("{\"state\":0");
			List<CameraPhotoBean> list = cameraService.findNormalPhotos(examineeId, roomId);
			sb.append(",\"rows\":");
			GsonFacade.toJson(list, sb);
			sb.append("}");
			ResponseUtil.renderJson(response, sb.toString());
			SinotnLogger.DEBUG.info(sb.toString());
		} catch (Exception e) {
			SinotnLogger.DEBUG.error("查看正常监考照片时发生系统错误", e);
			ResponseUtil.renderJson(response, "{\"status\":1,\"errorMsg\":\"查看正常监考照片时发生系统错误\"}");
		}
	}
}
